//
//  DDCkfzVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/8/2.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDCkfzVC.h"
#import "ASFTableView.h"
#import "DDNetWorkAPIClient.h"
#import "DDQuestionAnswerList.h"
#import "DDQuestionAnswerReply.h"
@interface DDCkfzVC ()<ASFTableViewDelegate>

@property (weak, nonatomic) IBOutlet ASFTableView *tableView;
@property (nonatomic, retain) NSMutableArray *rowsArray;
@end

@implementation DDCkfzVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"查看分数", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    
    [self AddLeftBtnAction:@"" normal:@"top_返回" selected:@"" action:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}

-(void)createData
{
    
    
    __weak DDCkfzVC *weakSelf = self;
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network mainQuestionAnswerList:@{@"topicId":_topic.id,@"pageIndex":@"0",@"pageSize":@"10000"} completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        
        DDQuestionAnswerList *mylist = (DDQuestionAnswerList*)object;
                _rowsArray = [[NSMutableArray alloc] init];
                NSArray *cols = @[@"题目编号",@"状态",@"得分"];
                NSArray *weights = @[@(0.33f),@(0.33f),@(0.34f)];
                NSDictionary *options = @{kASF_OPTION_CELL_TEXT_FONT_SIZE : @(16),
                                          kASF_OPTION_CELL_TEXT_FONT_BOLD : @(true),
                                          kASF_OPTION_CELL_BORDER_COLOR : [UIColor lightGrayColor],
                                          kASF_OPTION_CELL_BORDER_SIZE : @(1.0),
                                          kASF_OPTION_BACKGROUND : RGB(222, 222, 222)};
        
                [weakSelf.tableView setDelegate:self];
                [weakSelf.tableView setBounces:YES];
                [weakSelf.tableView setSelectionColor:[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0f]];
                [weakSelf.tableView setTitles:cols
                              WithWeights:weights
                              WithOptions:options
                                WitHeight:32 Floating:YES];
                 
                [weakSelf.rowsArray removeAllObjects];
        
        int sumFz = 0;
                for (int i=0; i<mylist.list.count; i++) {
                    DDQuestionAnswerListCell * cell = mylist.list[i];
                    NSString *state = cell.result;
                    
                    int fz = [cell.question.score intValue];
                    
                    //0-未做 1-正确 2-错误 3-主观已评分 4-主观未评分 5-无效回答
                    if ([state isEqualToString:@"0"]) {
                        state = @"未做";
                        fz = 0;
                    }
                    else if ([state isEqualToString:@"1"])
                    {
                        state = @"正确";
                    }
                    else if([state isEqualToString:@"2"])
                    {
                        state = @"错误";
                        fz = 0;
                    }
                    else if([state isEqualToString:@"3"])
                    {
                        state = @"主观已评分";
                        fz = 0;
                    }
                    else if([state isEqualToString:@"4"])
                    {
                        state = @"主观未评分";
                        fz = 0;
                    }
                    else if([state isEqualToString:@"5"])
                    {
                        state = @"无效回答";
                        fz = 0;
                    }
                    
                    sumFz += fz;
        
                    [weakSelf.rowsArray addObject:@{
                                            kASF_ROW_ID :
                                                @(i),
        
                                            kASF_ROW_CELLS :
                                                @[@{kASF_CELL_TITLE : [NSString stringWithFormat:@"%d",i+1], kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)},
                                                  @{kASF_CELL_TITLE : state, kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)},
                                                  @{kASF_CELL_TITLE :fz == 0 ? @"-" :[NSString stringWithFormat:@"%d",fz], kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)}
                                                  ],
        
                                            kASF_ROW_OPTIONS :
                                                @{kASF_OPTION_BACKGROUND : [UIColor whiteColor],
                                                  kASF_OPTION_CELL_PADDING : @(5),
                                                  kASF_OPTION_CELL_BORDER_COLOR : [UIColor lightGrayColor]},
                                            
                                            @"some_other_data" : @(123)}];
                }
        
        //合计
        [weakSelf.rowsArray addObject:@{
                                        kASF_ROW_ID :
                                            @(mylist.list.count+1),
                                        
                                        kASF_ROW_CELLS :
                                            @[@{kASF_CELL_TITLE : @"-", kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)},
                                              @{kASF_CELL_TITLE : @"合计", kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)},
                                              @{kASF_CELL_TITLE :[NSString stringWithFormat:@"%d",sumFz], kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)}
                                              ],
                                        
                                        kASF_ROW_OPTIONS :
                                            @{kASF_OPTION_BACKGROUND : [UIColor whiteColor],
                                              kASF_OPTION_CELL_PADDING : @(5),
                                              kASF_OPTION_CELL_BORDER_COLOR : [UIColor lightGrayColor]},
                                        
                                        @"some_other_data" : @(123)}];
        
                
                [weakSelf.tableView setRows:weakSelf.rowsArray];

        
    }];
    
//   DDNetWorkAPIClient * network = [[DDNetWorkAPIClient alloc] init];
//    [network giftQuery:@{@"logType":@"1"} completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
//        NSLog(@"%@",dict);
//        NSError *error ;
//        DDGiftLog *log = [[DDGiftLog alloc] initWithDictionary:dict error:&error];
//        
//        if (error) {
//            NSLog(@"%@",error.userInfo);
//        }
//        
//        _rowsArray = [[NSMutableArray alloc] init];
//        NSArray *cols = @[@"题目编号",@"状态",@"得分"];
//        NSArray *weights = @[@(0.33f),@(0.33f),@(0.34f)];
//        NSDictionary *options = @{kASF_OPTION_CELL_TEXT_FONT_SIZE : @(16),
//                                  kASF_OPTION_CELL_TEXT_FONT_BOLD : @(true),
//                                  kASF_OPTION_CELL_BORDER_COLOR : [UIColor lightGrayColor],
//                                  kASF_OPTION_CELL_BORDER_SIZE : @(1.0),
//                                  kASF_OPTION_BACKGROUND : RGB(222, 222, 222)};
//        
//        [_tableView setDelegate:self];
//        [_tableView setBounces:YES];
//        [_tableView setSelectionColor:[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0f]];
//        [_tableView setTitles:cols
//                      WithWeights:weights
//                      WithOptions:options
//                        WitHeight:32 Floating:YES];
//        
//        [_rowsArray removeAllObjects];
//        for (int i=0; i<log.list.count; i++) {
//            DDGiftLogCell *cell = log.list[i];
//            NSString *fenzhi =  cell.integral==nil?@"":cell.integral;;
//            
//            
//            [_rowsArray addObject:@{
//                                    kASF_ROW_ID :
//                                        @(i),
//                                    
//                                    kASF_ROW_CELLS :
//                                        @[@{kASF_CELL_TITLE : cell.logTime==nil?@"":cell.logTime, kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)},
//                                          @{kASF_CELL_TITLE : cell.desc==nil?@"":cell.desc, kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentLeft)},
//                                          @{kASF_CELL_TITLE :fenzhi, kASF_OPTION_CELL_TEXT_ALIGNMENT : @(NSTextAlignmentCenter)}
//                                          ],
//                                    
//                                    kASF_ROW_OPTIONS :
//                                        @{kASF_OPTION_BACKGROUND : [UIColor whiteColor],
//                                          kASF_OPTION_CELL_PADDING : @(5),
//                                          kASF_OPTION_CELL_BORDER_COLOR : [UIColor lightGrayColor]},
//                                    
//                                    @"some_other_data" : @(123)}];
//        }
//        
//        [_mASFTableView setRows:_rowsArray];
//        
//    }];
    
    
    
    
}
#pragma mark - ASFTableViewDelegate
- (void)ASFTableView:(ASFTableView *)tableView DidSelectRow:(NSDictionary *)rowDict WithRowIndex:(NSUInteger)rowIndex {
    NSLog(@"%d", rowIndex);
}
@end
